import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2022.6.29 15:37
 */
public class ConsistencyHashNoVirtual {
    public static void main(String[] args) {
        HashInfo hashInfo = new HashInfo();
        for (String server : hashInfo.servers) {
            hashInfo.serverMap.put(Math.abs(server.hashCode()) % 4294967295L, server);
        }

        for (String id : hashInfo.ids) {
            Long idHash = id.hashCode() % 4294967295L;;
            SortedMap<Long, String> hitServerMap = hashInfo.serverMap.tailMap(idHash);
            if (hitServerMap.isEmpty()) {
                System.out.println("id" + id + "被路由到" + hashInfo.serverMap.firstKey() + "(" + hashInfo.serverMap.get(hashInfo.serverMap.firstKey()) + ")服务器");
            } else {
                System.out.println("id" + id + "被路由到" + hitServerMap.firstKey() + "(" + hashInfo.serverMap.get(hitServerMap.firstKey()) + ")服务器");
            }
        }
    }
}
